package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.example.demo.pojo.Mysell;
import com.example.demo.pojo.Order;
import com.example.demo.pojo.OrderParamaccept;
import com.example.demo.pojo.OrdersGoods;
import com.example.demo.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.logging.Logger;

/**
 * @author yanglup
 * @create 2020-11-23 15:57
 */
@RestController
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    Logger log= Logger.getLogger("获取值");
    /**获取token*/
//    @RequestMapping("/my/orders/create")
//    public String gettoken(@RequestParam String code, @RequestParam String from){
//       return "123";
//    }
    /**付款*/




    /**创建订单--同时将状态变为1*/
    @PostMapping("/my/orders/create")
    public void createorders(@RequestBody JSONObject goods) {
        System.out.println(goods);
        OrderParamaccept ordersGoods=JSON.parseObject(goods.toJSONString(),OrderParamaccept.class);//解析为对象

        String openidbuy=ordersGoods.getOpenid();
        String consignee_addr=ordersGoods.getConsignee_addr();
        Integer order_price=ordersGoods.getOrder_price();

        //使用迭代器
        List<OrdersGoods> list=ordersGoods.getGoods();
        Iterator<OrdersGoods> iterator=list.iterator();
        while (iterator.hasNext()){
            OrdersGoods ordersGoodsitera=iterator.next();
            Integer goods_id=ordersGoodsitera.getGoods_id();
            Integer goods_number=ordersGoodsitera.getGoods_number();
            Integer goods_price=ordersGoodsitera.getGoods_price();
            ordersService.createorderstable(openidbuy,consignee_addr,order_price,goods_id,goods_number,goods_price);
            //修改商品的状态 变为待发货
            ordersService.updatestatus(goods_id);
        }
    }

    /**待发货----*/
    @RequestMapping("/my/orders/awaitorders")
    public Map<String,Object> awaitorders(String openid){

        String openidbuy=openid;
        System.out.println(openidbuy);
        List<Order> list=ordersService.awaitorders(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**待收货*/
    @RequestMapping("/my/orders/awairece")
    public Map<String,Object> awairece(String openid){
        String openidbuy=openid;
        List<Order> list=ordersService.awairece(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**待退货*/
    @RequestMapping("/my/orders/tuikuan")
    public Map<String,Object> tuikuan(String openid){
        String openidbuy=openid;
        List<Order> list=ordersService.tuikuan(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**全部订单*/
    @RequestMapping("/my/orders/allorders")
    public Map<String,Object> allorders(String openid){
        String openidbuy=openid;
        List<Order> list=ordersService.allorders(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**修改为退款状态*/
    @RequestMapping("/my/orders/clicktuikuan")
    public Map<String,Object> clicktuikuan(Integer goods_id,String openid){
        ordersService.clicktuikuan(goods_id);
        String openidbuy=openid;
        List<Order> list=ordersService.awairece(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }

    /**修改为确认状态*/
    @RequestMapping("/my/orders/confirmget")
    public Map<String,Object> confirmget(Integer goods_id,String openid){
        String openidbuy=openid;
        ordersService.confirmget(goods_id);

        List<Order> list=ordersService.awairece(openidbuy);

        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;

    }



    /**买方申请退货1---*/
    @RequestMapping("/my/orders/applyreturngoods")
    public void applyreturngoods(String openid,Integer goods_id){
        String openidbuy=openid;
        System.out.println(openidbuy);
//        System.out.println(goods_id);
        ordersService.applyreturngoods(openidbuy,goods_id);
    }

/**--------------------------下面是卖家的------------------------------*/
/**考虑代码复用*/
    /**待发货----*/
    @RequestMapping("/my/orders/selldaifahuo")
    public Map<String,Object> selldaifahuo(String openid){
        List<Order> list=ordersService.selldaifahuo(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }
    /**待收货*/
    @RequestMapping("/my/orders/sellyifahuo")
    public Map<String,Object> sellyifahuo(String openid){
        List<Order> list=ordersService.sellyifahuo(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }
    /**待退货*/
    @RequestMapping("/my/orders/selltuihuo")
    public Map<String,Object> selltuihuo(String openid){
        List<Order> list=ordersService.selltuihuo(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }
    /**全部订单*/
    @RequestMapping("/my/orders/sellallgoods")
    public Map<String,Object> sellallgoods(String openid){
        List<Order> list=ordersService.sellallgoods(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;
    }




    /**卖家已发货---这里的都是openid*/
    @RequestMapping("/my/orders/delivergoods")
    public Map<String,Object> delivergoods(Integer goods_id,String openid){

        ordersService.delivergoods(goods_id);
        List<Order> list=ordersService.selldaifahuo(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;


    }
    /**卖家同意退货---这里的都是openid*/
    @RequestMapping("/my/orders/agreereturn")
    public Map<String,Object> agreereturn(String openid,Integer goods_id){
        ordersService.agreereturn(openid,goods_id);
        List<Order> list=ordersService.selltuihuo(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
        map.put("meta", mymap);
        return map;

    }


/**--------------------------------mysell------------------------------------------------*/
    @RequestMapping("/my/mysell")
    public  Map<String,Object> mysell(String openid){
        List<Mysell> list= ordersService.mysell(openid);
//        Integer count=ordersService.mysellcount(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("message", list);
//        map.put("notsell", count);
        map.put("meta", mymap);
        return map;
    }
    @RequestMapping("/my/mysellcount")
    public  Map<String,Object> mysellcount(String openid){
        Integer count=ordersService.mysellcount(openid);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("msg", "获取成功");
        mymap.put("status", 200);
        Map<String, Object> map = new HashMap<>();
        map.put("notsell", count);
        map.put("meta", mymap);
        return map;
    }


}
